import 'package:extended_image/extended_image.dart';
import 'package:flutter/material.dart';


extension TColors on Colors{
  /// s: #FFFFFF
  static Color hexToColor(String s) {
    // 如果传入的十六进制颜色值不符合要求，返回默认值
    if (s == null || s.length != 7 || int.tryParse(s.substring(1, 7), radix: 16) == null) {
      s = '#999999';
    }
    return new Color(int.parse(s.substring(1, 7), radix: 16) + 0xFF000000);
  }
}
extension TImage on Image{
  static Image get userDefaultHeaderImage => Image.asset('images/shop_plashoad_img.png');
}
extension TImageProvider on ImageProvider{
  static ImageProvider get userDefaultHeaderImageProvider => AssetImage('images/user_logo_icon.png');
}
extension TString on String{
  Widget getImage({BoxFit fit = BoxFit.cover,double width,double height,bool circle=false}){
    if(this.isNotEmpty && this.contains('http')){
      return ExtendedImage.network(this,cache: true,fit: fit,width: width,height: height,
        shape:circle?BoxShape.circle:null,
      );
    }else if(this.isNotEmpty && this.length > 0 && this.indexOf('http') == -1 && this.contains('.file')){
//      return ExtendedImage.network('${RequestPaths.imageUrlBase}/$this',cache: true,fit: fit,width: width,height: height,);
    }else if(this.isNotEmpty && this.length > 0){
      return Image.asset((this),fit: fit,);
    }else if(this.isEmpty){
      return TImage.userDefaultHeaderImage;
    }else{
      return TImage.userDefaultHeaderImage;
    }
  }
  ImageProvider getImageProvider(){
    if(this.isNotEmpty && this.contains('http')){
      return NetworkImage(this);
    }else if(this.isNotEmpty && this.length > 0){
      return AssetImage(this);
    }else{
      return TImageProvider.userDefaultHeaderImageProvider;
    }
  }

  DateTime dateTime(){
    try{
      print("时间"+int.parse(this).toString());
      int dt=int.parse(this)*1000;
      var dateTime = DateTime.fromMillisecondsSinceEpoch(dt);
      print("时间"+dateTime.toString());
      return dateTime;
    }catch(e){
      print("时间错误");
      return DateTime.now();
    }
  }
  String xiaoQuanArtcleCreateTime(){
    var createTime = this.dateTime();
    var now = DateTime.now();
    var diff = now.difference(createTime);
    int sec= diff.inSeconds;
    int min= diff.inMinutes;
    int hor= diff.inHours;
    int day= diff.inDays;
    if(sec<60){
      return sec.toString()+"s";
    }else if(sec>=60&&min<60){
      return min.toString()+"分钟之前";
    }else if(min>=60&&hor<24){
      return hor.toString()+"小时之前";
    }else if(hor>=24){
      return day.toString()+"天之前";
    }
    return this;
  }
}